package com.mall.consumer;

import com.mall.pojo.order.Order;
import com.mall.service.order.OrderService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单入库
 * 当rpc调用扣减库存成功后再次将订单入库
 */
@Service
@RocketMQMessageListener(topic = "tx_order_msg", consumerGroup = "tx_order")
public class OrderConsumer  implements RocketMQListener<Order>, RocketMQPushConsumerLifecycleListener {

    @Resource
    private OrderService orderService;
    // 监听消息进行减库存
    @Override
    public void onMessage(Order order ) {
        orderService.insertOrder(order);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
    }
}
